//
//  String+Emphasized.swift
//  TestApp
//
//  Created by hongyan.yang on 2022/4/29.
//

import UIKit

extension String {
    
    func setEmphasizedText(_ emphasizedText: String, regularAttributes:  [NSAttributedString.Key : Any], emphasizedAttributes:  [NSAttributedString.Key : Any]) -> NSAttributedString {
        let attributedString = NSMutableAttributedString(string: self, attributes: regularAttributes)
        guard !self.isBlank, !emphasizedText.isBlank else{
            return attributedString
        }
        
        var index = 0
        // Loop through string looking for emphasizedText
        for char in self {
            // Avoid exceed string's index
            guard emphasizedText.count + index <= self.count else {
                break
            }
            if emphasizedText.first == char {
                let startIndex = self.index(self.startIndex, offsetBy: index)
                let endIndex = self.index(self.startIndex, offsetBy: (emphasizedText.count + index))
                let range = startIndex..<endIndex
                if String(self[range]) == emphasizedText {
                    // update emphasizedAttributes in attributedString
                    attributedString.beginEditing()
                    attributedString.addAttributes(emphasizedAttributes, range:NSRange(range, in: self))
                    attributedString.endEditing()
                }
            }
            index += 1
        }
        return attributedString
    }
    func setEmphasizedRange(_ range: Range<String.Index>, regularAttributes: [NSAttributedString.Key: Any], emphasizedAttributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
        let attributedString = NSMutableAttributedString(string: self, attributes: regularAttributes)
        guard !self.isBlank else{
            return attributedString
        }
        if !String(self[range]).isEmpty {
            attributedString.beginEditing()
            attributedString.addAttributes(emphasizedAttributes, range: NSRange(range, in: self))
            attributedString.endEditing()
        }
        return attributedString
    }
}


extension UILabel {
    func setEmphasizedRange(_ range: Range<String.Index>, emphasizedFont: UIFont, emphasizedColor: UIColor = .black) {
        let emphasizedAttributes = [NSAttributedString.Key.font: emphasizedFont, NSAttributedString.Key.foregroundColor: emphasizedColor]
        // emphasizedText's attributes
        setEmphasizedRange(range, emphasizedAttributes: emphasizedAttributes)
    }
    func setEmphasizedRange(_ range: Range<String.Index>, emphasizedAttributes: [NSAttributedString.Key : Any]) {
        guard let string = self.text, !string.isBlank else{
            return
        }
        // general attributes
        let regularAttributes = [NSAttributedString.Key.font: self.font!, NSAttributedString.Key.foregroundColor: self.textColor!]
        
        let attributedString = string.setEmphasizedRange(range, regularAttributes: regularAttributes, emphasizedAttributes: emphasizedAttributes)
        
        self.attributedText = attributedString
    }
    
    func setEmphasizedText(_ emphasizedText: String, emphasizedFont: UIFont, emphasizedColor: UIColor = .black) {
        let emphasizedAttributes = [NSAttributedString.Key.font: emphasizedFont, NSAttributedString.Key.foregroundColor: emphasizedColor]
        // emphasizedText's attributes
        setEmphasizedText(emphasizedText, emphasizedAttributes: emphasizedAttributes)
    }
    
    func setEmphasizedText(_ emphasizedText: String, emphasizedAttributes: [NSAttributedString.Key : Any]) {
        guard let string = self.text, string.isBlank != true else{
            return
        }
        // general attributes
        let regularAttributes = [NSAttributedString.Key.font: self.font!, NSAttributedString.Key.foregroundColor: self.textColor!]
        
        let attributedString = string.setEmphasizedText(emphasizedText, regularAttributes: regularAttributes, emphasizedAttributes: emphasizedAttributes)
        
        self.attributedText = attributedString
    }
    func setUnderlinedText(_ emphasizedText: String, emphasizedFont: UIFont, emphasizedColor: UIColor = .black) {
        let emphasizedAttributes = [NSAttributedString.Key.font: emphasizedFont, NSAttributedString.Key.foregroundColor: emphasizedColor, NSAttributedString.Key.underlineStyle: 1] as [NSAttributedString.Key : Any]
        // emphasizedText's attributes
        setEmphasizedText(emphasizedText, emphasizedAttributes: emphasizedAttributes)
    }
}
extension UITextView {
    func setLinkedText(_ linkedText: String, linkTo: String, linkedColor: UIColor = .black) {
        let linkedAttributes: [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.link: linkTo,
            NSAttributedString.Key.underlineStyle: 1
        ]
        // set textview's property properly, or the color of link doesn't work
        self.linkTextAttributes = [NSAttributedString.Key.foregroundColor: linkedColor]
        // emphasizedText's attributes
        setLinkedText(linkedText, linkTo: linkTo, linkedAttributes: linkedAttributes)
    }
    func setLinkedText(_ linkedText: String, linkTo: String, linkedAttributes: [NSAttributedString.Key : Any]) {
        guard let string = self.text, string.isBlank != true else{
            return
        }
        // general attributes
        let regularAttributes = [NSAttributedString.Key.font: self.font!, NSAttributedString.Key.foregroundColor: self.textColor!]
        
        let attributedString = string.setEmphasizedText(linkedText, regularAttributes: regularAttributes, emphasizedAttributes: linkedAttributes)
        
        self.attributedText = attributedString
    }
}
